package ink.lovejinhu.leetcode;

/**
 * @author jinhu
 * created 2021-08-17 19:49
 */
public class Solution_0004 {
    /**
     * 寻找两个正序数组中位数
     *
     * @param nums1
     * @param nums2
     * @return
     */
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int[] ints = new int[nums1.length + nums2.length];
        int k = 0;
        int i = 0, j = 0;
        for (; i < nums1.length && j < nums2.length; ) {
            if (nums1[i] >= nums2[j]) {
                ints[k] = nums2[j];
                j++;
            } else {
                ints[k] = nums1[i];
                i++;
            }
            k++;
        }

        if (i == nums1.length) {
            for (int t = j; t < nums2.length; t++) {
                ints[k] = nums2[t];
                k++;
            }
        }
        if (j == nums2.length) {
            for (int l = i; l < nums1.length; l++) {
                ints[k] = nums1[l];
                k++;

            }
        }
        return ints.length % 2 == 0 ? (double) (ints[ints.length / 2] + ints[ints.length / 2 - 1]) / 2 : (double) ints[ints.length / 2];

    }
}
